﻿//========================================
// All rights reserved (C) 2018-2100
// CLR 版本：	4.0.30319.42000
// 命名空间：	SFJDService
// 文 件 名：	LogBoxBase
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2019/3/11 22:53:52
//========================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

namespace SFJDService
{
    public class LogBoxBase : TextBlock
    {
        IString sb = new IString();
        Point startMousePosition;
        TextPointer startPt;

        public LogBoxBase()
        {
            //sb.Text = "1";
            //Binding binding = new Binding(".") { Source = sb, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
            Binding binding = new Binding("Text") { Source = sb, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
            SetBinding(TextProperty, binding);
            TextWrapping = TextWrapping.Wrap;
            TextTrimming = TextTrimming.CharacterEllipsis;

            //选择文件功能
            PreviewMouseLeftButtonDown += LogBox_PreviewMouseLeftButtonDown;
            PreviewMouseLeftButtonUp += LogBox_PreviewMouseLeftButtonUp;
            //DataContext = sb;

            //Background = Brushes.Gray;
        }

        private void LogBox_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (startPt == null) return;
            Point lastMousePosition = e.GetPosition(this);
            TextPointer tp = GetPositionFromPoint(lastMousePosition, true);
            TextRange tr = new TextRange(startPt, tp);
            string seltext = tr.Text;
        }

        private void LogBox_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            startMousePosition = e.GetPosition(this);
            startPt = GetPositionFromPoint(startMousePosition, true);
        }

        public void WriteLine(string str)
        {
            if (sb.Text.Length > 102400)
            {
                sb.Text = "";
            }
            DateTime dt = DateTime.Now;
            sb.Text += " " + dt.ToString("HH:mm:ss") + " " + str + "\n";
        }

        private class IString : INotifyPropertyChanged
        {
            private string text = "";
            public string Text
            {
                get { return text; }
                set
                {
                    if (text != value)
                    {
                        text = value;
                        PropertyChanged(this, new PropertyChangedEventArgs("Text"));
                    }
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
        }
    }
}
